﻿using System.Diagnostics;
using System.Text;

namespace 语文字帖
{
    public class CommandShell
    {
        private Process proc = null;
        public CommandShell()
        {
        }

        public string Echo => txt.ToString();

        private readonly StringBuilder txt = new StringBuilder();
        public void RunCmd(string cmd, bool isEchoNow = true)
        {
            proc = new Process();
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.FileName = "cmd.exe";
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.RedirectStandardInput = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.Start();
            proc.StandardInput.WriteLine(cmd);
            proc.StandardInput.WriteLine("exit");
            while (!proc.StandardOutput.EndOfStream)
            {
                string s = proc.StandardOutput.ReadLine();
                txt.AppendLine(s);
            }
            proc.Close();
        }
    }
}
